package com.spring.starter.aspectJ;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tonywang
 * @date 6/15/2018
 */
//@Aspect
//@Component
public class TrackCounter {
    private Map<String, Integer> trackCount = new HashMap<>();
//    @Pointcut("execution(* com.spring.starter.CompactDisc.say(String)) && args(trackNumber)")
    public void trackPlay(String trackNumber){}
//    @Before("trackPlay(trackNumber)")
    public void countTrack(String trackNumber){
        int currentCount = getPlayerCount(trackNumber);
        trackCount.put(trackNumber, currentCount +1);
    }
//    @AfterReturning("trackPlay(trackNumber)")
    public void sayCount(String trackNumber){
        System.out.println("trackCount: after " + trackNumber + trackCount.get(trackNumber));
    }
    public int getPlayerCount(String trackNumber) {
        return trackCount.containsKey(trackNumber) ? trackCount.get(trackNumber) : 0;
    }
}
